/*
 * @Author: xueyp
 * @Date: 2020-06-10 20:11:05
 * @Last Modified by: xueyp
 * @Last Modified time: 2020-06-10 20:47:40
 * @description: 一些常用的工具
 */
import store from '@/Vuex/index'
import moment from 'moment'
// 格式化时间戳
export const formatterTime = (type, time) => {
  var date = new Date()
  date.setTime(time || Date.now())
  var y = date.getFullYear().toString().padStart(2, '0')
  var m = (date.getMonth() + 1).toString()
  var d = date.getDate().toString().padStart(2, '0')
  var ss = date.getSeconds().toString().padStart(2, '0')
  var hh = date.getHours().toString().padStart(2, '0')
  var mm = date.getMinutes().toString().padStart(2, '0')
  switch (type) {
    case 'yy-mm-dd hh:mm': return `${y}-${m}-${d} ${hh}:${mm}`; break;
    case 'yy-mm-dd': return `${y}-${m}-${d}`; break;
    case 'yy/mm/dd hh:mm': return `${y}/${m}/${d} ${hh}:${mm}`; break;
    case 'yy/mm/dd': return `${y}/${m}/${d}`; break;
    case 'hh:mm': return `${hh}:${mm}`; break;
    case 'hh:mm:ss': return `${hh}:${mm}:${ss}`; break;
    case 'mm/dd hh:mm:ss': return `${m}/${d} ${hh}:${mm}:${ss}`; break;
    case 'mm/dd hh:mm': return `${m}/${d} ${hh}:${mm}`; break;
    case 'mm月dd日 hh:mm': return `${m}月${d}日 ${hh}:${mm}`; break;
    case 'mm月dd日 hh:mm:ss': return `${m}月${d}日 ${hh}:${mm}:${ss}`; break;
  }
}

// 判断当前是上午、下午还是晚上
export const judgeCurrentTime = () => {
  let timeNow = new Date(store.state.serverTime);
  let hours = timeNow.getHours();
  let text = {};
  if (hours >= 4 && hours <= 10) {
    text = { name: 'Hi,早上好', value: 0 };
  } else if (hours > 10 && hours <= 14) {
    text = { name: 'Hi,中午好', value: 1 };
  } else if (hours > 14 && hours <= 18) {
    text = { name: 'Hi,下午好', value: 2 };
  } else if (hours > 18 || hours <= 4) {
    text = { name: 'Hi,晚上好', value: 3 };
  }
  return text;
}

export const getServerTime = (time) => {
  return moment(time).format('MM[月] DD[日] HH:mm:ss')
}